<?php

namespace backend\modules\personnel\controllers;

use common\models\personnel\Personnel;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\personnel\Grade;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

/**
 *等级管理
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class GradeController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Grade::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $id=Yii::$app->request->get('id');
//        $data = self::actionStatistics($id);
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['position'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);

        $dataProvider->query
            ->where(['id'=>$id])
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'user_id'=>$id,
//            'statistics'=>$data,
        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $data = Yii::$app->request->post();
        //var_dump($data);die();
        if ($model->load($data) && $model->save()) {
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'model' => $model,
        ]);
    }
    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $sid= Yii::$app->request->get('sid');
        $model = $this->findModel($id);
        //var_dump(Yii::$app->request->post());die();
       // $model->user=$sid;
        //$this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }
        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'menuDropDownList' => Yii::$app->services->menu->getDropDownts(),
            'user_id'=>$sid,
        ]);
    }
}